package com.xxxx.server.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.page.RespPageBean;
import com.xxxx.server.pojo.Employee;

import com.xxxx.server.pojo.EmployeeAdd;
import com.xxxx.server.pojo.EmployeeImport;
import com.xxxx.server.service.IEmployeeService;

//import com.xxxx.server.utils.DateUtil;
//import com.xxxx.server.utils.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    public final static Logger logger = LoggerFactory.getLogger(IEmployeeService.class);
    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    @Resource
    private  EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender mailSender;


    //展示所有数据
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, Date[] beginDateScope) {
        if (currentPage != null && size != null) {
            currentPage = (currentPage - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(currentPage, size, employee, beginDateScope);
        Long total = employeeMapper.getTotal(employee, beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    //更新员工信息
    @Override
    public Integer updateEmployeeById(Employee employee) {
        return employeeMapper.updateEmployeeById(employee);
    }




    //导出数据
    @Override
    public List<Employee> findAllEmployee() {
        return employeeMapper.findAllEmployee();
    }

    //根据ID删除员工信息
    @Override
    public Integer removeEmployeeById(Integer id) {
        return employeeMapper.deleteEmployeeById(id);
    }


    //添加员工信息
    @Override
    public Integer saveEmployee(EmployeeAdd employeeAdd) {
        Date beginContract = employeeAdd.getBeginContract();
        Date endContract = employeeAdd.getEndContract();
        Employee employee = new Employee();

        double month = (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract))) * 12 + (Double.parseDouble(monthFormat.format(endContract)) - Double.parseDouble(monthFormat.format(beginContract)));
        employeeAdd.setContractTerm(Double.parseDouble(decimalFormat.format(month / 12)));
        int result = employeeMapper.addEmployeeOne(employeeAdd);
        //如果添加成功就获取到邮箱 发送邮件
        if (result == 1) {
            Employee email = employeeMapper.getEmployeeById(employeeAdd.getId());

            //发送邮件
            //sendHtmlMail("18731219812@163.com","主题：你好text邮件","内容：第一封text邮件");
            sendHtmlMail(email.getEmail(),"主题：你好text邮件","内容：第一封text邮件");
        }
        return result;
    }

    //最大工号  完成添加时的自增
    public Integer maxWorkID() {
        return employeeMapper.maxWorkID();
    }


    //配置文件中我的QQ邮箱 邮件发送者
    @Value("${spring.mail.from}")
    private String from;

    //按照员工的 id对员工发送邮件
    public Employee getEmployeeById(Integer empId) {
        return employeeMapper.getEmployeeById(empId);
    }


    //发送邮件
    public void sendHtmlMail(String to, String subject, String content) {
        //获取Message对象
        SimpleMailMessage message = new SimpleMailMessage();
        Employee employee = new Employee();
        try {
            //邮件发送人
            message.setFrom(from);
            //邮件接收人
            message.setTo(to);
            //设置抄送地址，如需设置多个地址，可以使用setCc(String[] cc)方法
            //message.setCc(employee.getEmail());
            //设置暗送地址，如需设置多个地址，可以使用setBcc(String[] bcc)方法
            //message.setBcc(employee.getEmail());
            //邮件主题
            message.setSubject(subject);
            //邮件内容，html格式
            message.setText(content);
            //发送
            mailSender.send(message);
            //日志信息
            logger.info("邮件已经发送");
        } catch (Throwable e) {
            logger.error("邮件发送失败", e);
        }

    }

    @Override
    public void readExcel(List<EmployeeImport> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        for(EmployeeImport employee : list){
            System.out.println("employee=" + employee);
        }
    }


    @Override
    public void save(List<EmployeeImport> list) {

        employeeMapper.saveBatch(list);
    }

}
